﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SQLite;
using Com.Toook.AutoDownloadHtml.Util;

public partial class EditDownload_Joke_JokeTempList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitJoke();
        }
    }
    private string ReplaceText(string text)
    {
        string[,] commonText = {{"&nbsp;", ""}, 
                                {"\\s+", " "},
                                {"<font>", ""},
                                {"</font>", ""},                                
                                {"<font face=\"Verdana\">", ""}
                               };
        
        for (int i = 0; i < commonText.Length / 2; i++)
        {
            string a = commonText[i, 0];
            string b = commonText[i, 1];
            text = text.Replace(a, b);
        }
        return text;
    }
    private void InitJoke()
    {
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        string sql = "select id, siteKindId, title, content from UncheckedJoke where edited = 0  limit 1 ";// +start.ToString() + ", 1 ";
        SQLiteDataReader reader = null;
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                original.Value = labelContentOriginal.Text = reader["content"].ToString();
                editResult.Text = txtEdit.Value = ReplaceText(original.Value);
                labelJokeId.Text = reader["id"].ToString();
                labelSiteKindId.Text = reader["siteKindId"].ToString();
                txtTitle.Text = reader["title"].ToString();
                
            }
            //rptJoke.DataSource = reader;
            //rptJoke.DataBind();            
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error("读取更新时间错误", ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
    }
   
    /// <summary>
    /// 显示下一条按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ShowNext();
    }
    /// <summary>
    /// 显示下一条
    /// </summary>
    private void ShowNext()
    {
        //int next;
        //if (!int.TryParse(txtStartNumber.Text, out next))
        //{
        //    next = 0;
        //}
        
        //txtStartNumber.Text = (next + 1).ToString();
        
        InitJoke();
    }
    /// <summary>
    /// 保存当前编辑内容到正式表，并显示下一条
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveAndNext_Click(object sender, EventArgs e)
    {
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        string sql = "insert into JokeContent(id, title, jokeType, level, content) values(@id, @title, @jokeType, @level, @content) ;" +
            " update UncheckedJoke set edited = 1 where id = " + labelJokeId.Text;  
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        cmd.Parameters.Add("@id", DbType.Int32).Value = SqliteHelper.GetJokeTypeId(DropDownListType.SelectedValue);
        cmd.Parameters.Add("@title", DbType.String, 30).Value = txtTitle.Text;
        cmd.Parameters.Add("@jokeType", DbType.Int32).Value = DropDownListType.SelectedValue;
        cmd.Parameters.Add("@level", DbType.String).Value = DropDownListLevel.SelectedValue;
        cmd.Parameters.Add("@content", DbType.String).Value = txtEdit.Value;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error("保存笑话编辑结果错误", ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, null);
        }

        ShowNext();
    }
    
    /// <summary>
    /// 删除当前编辑内容，并显示下一条
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDeleteAndNext_Click(object sender, EventArgs e)
    {
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        string sql = "delete from UncheckedJoke where id = " + labelJokeId.Text;        
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error("删除笑话错误。ID=" + labelJokeId.Text, ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, null);
        }

        ShowNext();
    }
}
